package com.gsma.calendar.android.widget;

import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.gsma.calendar.android.R;
import com.gsma.calendar.android.util.Config;

/**
 * Custom layout that contains and organizes a {@link TimeRulerView} and several
 * instances of {@link SessionView}. Also positions current "now" divider using
 * {@link R.id#sessions_now} view when applicable.
 */
public class SessionsLayout extends ViewGroup {

	private TimeRulerView mRulerView;
	private View mNowView;

	public SessionsLayout(Context context) {
		this(context, null);
	}

	public SessionsLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SessionsLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	private void ensureChildren() {
		mRulerView = (TimeRulerView) findViewById(R.id.sessions_ruler);
		if (mRulerView == null) {
			throw new IllegalStateException("Must include a R.id.sessions_ruler view.");
		}
		mRulerView.setDrawingCacheEnabled(true);

		mNowView = findViewById(R.id.sessions_now);
		if (mNowView == null) {
			throw new IllegalStateException("Must include a R.id.sessions_now view.");
		}
		mNowView.setDrawingCacheEnabled(true);
	}

	/**
	 * Remove any {@link SessionView} instances, leaving only
	 * {@link TimeRulerView} remaining.
	 */
	public void removeAllSessions() {
		ensureChildren();
		removeAllViews();
		addView(mRulerView);
		addView(mNowView);
	}

	public void addSession(SessionView sessionView, int position) {
		sessionView.setDrawingCacheEnabled(true);
		addView(sessionView);// , position);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		ensureChildren();

		mRulerView.measure(widthMeasureSpec, heightMeasureSpec);
		mNowView.measure(widthMeasureSpec, heightMeasureSpec);

		final int width = mRulerView.getMeasuredWidth();
		final int height = mRulerView.getMeasuredHeight();

		setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec));
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		ensureChildren();
		Set<Integer> processedIds = new HashSet<Integer>();

		final TimeRulerView rulerView = mRulerView;
		final int headerWidth = rulerView.getHeaderWidth();

		rulerView.layout(0, 0, getWidth(), getHeight());

		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			final View child = getChildAt(i);
			if (child.getVisibility() == GONE)
				continue;

			if (child instanceof SessionView) {
				final SessionView sessionView = (SessionView) child;
				Integer sessionId = Integer.valueOf(sessionView.getSessionId());
				if (!processedIds.contains(sessionId)) {
					final int top = rulerView.getTimeVerticalOffset(sessionView.getStartTime());
					final int bottom = rulerView.getTimeVerticalOffset(sessionView.getEndTime());

					final int columnWidth = (getWidth() - headerWidth) / sessionView.getColumns();
					final int left = sessionView.getColumns() == 1 ? headerWidth : headerWidth + (sessionView.getColumn() * columnWidth);
					final int right = sessionView.getColumns() == 1 ? getWidth() : left + columnWidth;

					if (((SessionView) child).getColumns() > 1) {
						// child.layout(top, right, bottom, left);
						child.layout(left, top, right, bottom);

						((SessionView) child).setTextSize(getResources().getDimension(R.dimen.text_size_small));
						child.setPadding(5, 5, 5, 5);

						/*
						 * RotateAnimation ranim =
						 * (RotateAnimation)AnimationUtils
						 * .loadAnimation(this.getContext(), R.anim.rotate);
						 * ranim.setFillAfter(true); child.setAnimation(ranim);
						 */
					} else {
						child.layout(left, top, right, bottom);
					}
					if (((SessionView) child).isBreak()) {
						((SessionView) child).setTextColor(getResources().getColor(R.color.black));
					}
					processedIds.add(sessionId);
				}
			}
		}

		// Align now view to match current time
		final View nowView = mNowView;
		// TODO
		// TimeZone.setDefault(Config.CONFERENCE_TIME_ZONE);
		final long now = System.currentTimeMillis();

		final int top = rulerView.getTimeVerticalOffset(now);
		final int bottom = top + nowView.getMeasuredHeight();
		final int left = 0;
		final int right = getWidth();

		nowView.layout(left, top, right, bottom);
	}
}
